package com.hnevc.trafficdemotest1.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hnevc.trafficdemotest1.R;
import com.hnevc.trafficdemotest1.entity.AccountManager;
import com.hnevc.trafficdemotest1.fragment.AccountManagerFragment;
import com.hnevc.trafficdemotest1.fragment.DataAnalysisFragment;
import com.hnevc.trafficdemotest1.fragment.LifeHelpFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    TextView mTvTitle;
    ListView mListView;
    ImageView mIvHome;

    String[] menuNameArray;
    int[] menuIconArray;
    List<Map<String, Object>> menuList;

    SlidingPaneLayout mSlidingPaneLayout;
    boolean flag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        initView();
    }

    private void initView() {

        //获取侧滑控件
        mSlidingPaneLayout = (SlidingPaneLayout) findViewById(R.id.spl_root);
        //获取home按键
        mIvHome = (ImageView) findViewById(R.id.header_back_iv);
        //标题部分
        mTvTitle = (TextView) findViewById(R.id.header_title_tv);

        mListView = (ListView) findViewById(R.id.id_lv_menu);
        final MyBaseMenuAdapter menuAdapter = new MyBaseMenuAdapter();
        mListView.setAdapter(menuAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mTvTitle.setText((String) menuList.get(i).get("name"));
//                String name = (String) menuList.get(i).get("name");
                switch (i) {
                    case 0://账户管理
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.id_content_view, new AccountManagerFragment())
                                .commit();
                        break;
                    case 1:
                        //公交查询


                        break;
                    case 2:
                        //红绿灯管理

                        break;
                    case 3:
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                    case 6:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.id_content_view,new LifeHelpFragment())
                                .commit();
                        break;
                    case 7:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.id_content_view, new DataAnalysisFragment())
                                .commit();
                        break;
                }
            }
        });
        //显示和掩藏侧滑菜单
        mIvHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flag) {
                    mSlidingPaneLayout.closePane();
                    flag = false;

                } else {
                    mSlidingPaneLayout.openPane();
                    flag = true;
                }
            }
        });
    }

    //初始化代码
    private void initData() {
        SharedPreferences sharedPreferences = getSharedPreferences("data", Context.MODE_PRIVATE);
        String role = sharedPreferences.getString("Role","");
        menuNameArray = getResources().getStringArray(R.array.menuarray);
        menuIconArray = new int[]{R.drawable.a, R.drawable.b, R.drawable.c,R.drawable.a, R.drawable.b, R.drawable.c,R.drawable.a, R.drawable.b, R.drawable.c};
        menuList = new ArrayList<>();
        for (int i = 0; i < menuNameArray.length; i++) {
            Map map = new HashMap();
            map.put("name", menuNameArray[i]);
            map.put("icon", menuIconArray[i]);
            menuList.add(map);
        }
        if (role.equals("R01")) {
            for (int i = 0; i < menuList.size(); i++) {
                if (menuList.get(i).get("name").equals("账户管理")) {
                    menuList.remove(i);
                }
                if (menuList.get(i).get("name").equals("红绿灯管理")) {
                    menuList.remove(i);
                }
            }
        }

    }

    class MyBaseMenuAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return menuList.size();
        }

        @Override
        public Object getItem(int i) {
            return menuList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = View.inflate(MainActivity.this, R.layout.menu_list_item, null);
            TextView mTvUserName = (TextView) view.findViewById(R.id.id_tv_menu_name);
            mTvUserName.setText((String) menuList.get(i).get("name"));
            ImageView mIvImageView = (ImageView) view.findViewById(R.id.id_iv_menu_icon);
            mIvImageView.setImageResource((Integer) menuList.get(i).get("icon"));
            return view;
        }
    }
}

